<?php
//适配器模式adapter

// 将一个类的接口转换成客户希望的另一个接口,适配器模式使得原本的由于接口不兼容而不能一起工作的那些类可以一起工作。
// 应用场景：老代码接口不适应新的接口需求，或者代码很多很乱不便于继续修改，或者使用第三方类库。例如：php连接数据库的方法：mysql,,mysqli,pdo,可以用适配器统一
//老代码
class User{
    private $name;
    function __construct($name){
        $this->name =$name;
    }
    public function getName(){
        return $this->name;
    }
}
//新代码，开放平台标准接口
interface UserInterface{
    function getUserName();
}
class  UserInfo implements UserInterface{
    protected $user;
    function __construct(User $user){
        $this->user=$user;
    }
    public function getUserName()
    {
        // TODO: Implement getUserName() method.
        return $this->user->getName();
    }
}
$olduser = new User('张三');

echo $olduser->getName()."n";

$newuser = new UserInfo($olduser);

echo $newuser->getUserName()."n";






//对象适配器
 interface  Target{
     public function sampleMethod1();
     public function sampleMethod2();
 }

 class Adaptee{
     public function sampleMethod1(){
         echo "sfsfsfs";
     }
 }


 class Adapter implements Target{
     private $Adaptee;
     public function __contruct(Adaptee $adaptee){
         $this->Adaptee= $adaptee->sampleMethod1();
     }
     public function sampleMethod1()
     {
         // TODO: Implement sampleMethod1() method.
         $this->Adaptee=$this->sampleMethod1();
     }
     public function sampleMethod2()
     {
         // TODO: Implement sampleMethod2() method.
         echo "dsfsfswewe232";
     }
 }

$adapter = new Adapter(new Adaptee());
$adapter->sampleMethod1();
$adapter->sampleMethod2();


//类适配器
interface Target2 {
    public function sampleMethod1();
    public function sampleMethod2();
}

class Adaptee2 { // 源角色
    public function sampleMethod1() {}
}

class Adapter2 extends Adaptee2 implements Target2 { // 适配后角色
    public function sampleMethod2() {}
}

$adapter = new Adapter2();
$adapter->sampleMethod1();
$adapter->sampleMethod2();
